<script lang="ts" setup>
import BigNumber from 'bignumber.js'

const emit = defineEmits(['close', 'show-topup', 'unlocked'])
const showDownload = ref(false)
const router = useRouter()
const { getUserInfo } = useUserStore()

const props = defineProps({
  filmInfo: {
    type: Object,
    default() {
      return {}
    },
  },
  showClose: {
    type: Boolean,
    default: true,
  },
})

function handleOpen() {
  emit('close')
  if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) {
    showToast('IOS app development in progress')
  } else {
    try {
      window.location.href = 'tv://tv.com:8088/router'
    } catch (error) {
    } finally {
      showDownload.value = true
    }
  }
}

async function handleUnlock() {
  const { Code, Data } = await useRequest.post('/UnlockFilmEpisode', {
    episodes: props.filmInfo?.curEpisode,
    film_id: props.filmInfo?.id,
    get_price: 0,
    auto: 1,
  })
  if (Code === 0) {
    const epInfo = Data?.unlock?.find((item: any) => item.episode_no === props.filmInfo.curEpisode)
    const videoSrc = epInfo?.urls?.find((item: any) => item.endsWith(`.m3u8`))
    const unlockList = props.filmInfo.unlocked_film_episodes ?? []
    unlockList.push(epInfo)
    getUserInfo()
    showToast('Unlocked!')
    emit('close')
    emit('unlocked', {
      ...props.filmInfo,
      videoSrc,
      episodeInfo: epInfo,
      unlocked_film_episodes: unlockList,
    })
  }
  if (Code === 6002) {
    emit('close')
    showConfirmDialog({
      title: 'Tips',
      message: 'Your balance is insufficient, please go to recharge',
      confirmButtonText: 'Confirm',
      cancelButtonText: 'Cancel',
    })
      .then(() => {
        router.push({
          path: '/recharge',
        })
      })
      .catch(() => {})
  }
}
</script>

<template>
  <div class="watch-popup">
    <!-- close -->
    <div
      v-if="showClose"
      class="close"
      @click="$emit('close')">
      <svg-icon
        name="close"
        class="icon">
      </svg-icon>
    </div>

    <!-- need-corns -->
    <div class="need-corns">
      <svg-icon name="w-lock"></svg-icon>
      <div
        class="corn-info"
        @click="handleUnlock">
        <div class="tip">Unlock this episode</div>
        <div class="corns">
          {{ BigNumber(props.filmInfo?.episodeInfo?.coins).div(10000) }} Coins
        </div>
      </div>
    </div>

    <!-- watch-btn -->
    <div
      class="watch-btn"
      @click="handleOpen">
      Watch this in APP
    </div>

    <!-- download-popup -->
    <van-popup
      v-model:show="showDownload"
      teleport="body">
      <LazyDownloadPopup @close="showDownload = false"> </LazyDownloadPopup>
    </van-popup>
  </div>
</template>

<style lang="scss" scoped>
.watch-popup {
  position: relative;
  width: 295px;
  height: 228px;
  padding: 84px 40px 40px;
  @include bgimg(cover, 'dialog_bg.png');
  .close {
    position: absolute;
    right: 5px;
    top: 0;
    .icon {
      font-size: 20px;
    }
  }
  .need-corns {
    @include flexbox($ai: center);
    padding: 16px;
    height: 40px;
    margin-bottom: 24px;
    border-radius: 4px;
    border: 1px solid #f93944;
    background: rgba(249, 57, 68, 0.2);
    .svg-icon {
      font-size: 20px;
      margin-right: 7px;
    }
    .corn-info {
      .tip {
        font-size: 15px;
        font-weight: 400;
        color: #fff;
      }
      .corns {
        font-size: 12px;
        color: #fff;
      }
    }
  }
  .watch-btn {
    width: 100%;
    height: 40px;
    line-height: 40px;
    text-align: center;
    border-radius: 4px;
    font-size: 16px;
    color: #fff;
    background-color: $primary-color;
  }
}
</style>
